package com.activenetwork.smlite.testcase.events.createEvents.basics;

import java.util.GregorianCalendar;
import java.util.List;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.activenetwork.schema.model.event.EventBasicType;
import com.activenetwork.schema.model.event.EventType;
import com.activenetwork.smlite.business.EventBusiness;
import com.activenetwork.smlite.testcase.BaseCase;
import com.activenetwork.util.EnvContext;
import com.activenetwork.util.verification.gson.FailurePoint;

public class Basics013 extends BaseCase {
	static final Logger logger = LoggerFactory.getLogger(Basics013.class);
	private EventBusiness eventBusiness = new EventBusiness();
	private EventType event;
	
	@BeforeClass(alwaysRun = true)
	public void dataSetup() throws DatatypeConfigurationException {
		// create an event
		event = new EventType();
		EventBasicType eventBasic = new EventBasicType();
		
		// event title
		eventBasic.setTitle("basics013");
		
		// event location
		EventBasicType.Location location = new EventBasicType.Location();
		location.setLocationName("smlite");
		eventBasic.setLocation(location);
		
		//event category
		eventBasic.setCategory("Expo");
		
		// event length
		EventBasicType.Length.MultiDay multi = new EventBasicType.Length.MultiDay();

		//Start date
		XMLGregorianCalendar startDateCal = DatatypeFactory.newInstance().newXMLGregorianCalendar(new GregorianCalendar());
		multi.setStartDate(startDateCal);
		
		//end date
		XMLGregorianCalendar endDateCal = DatatypeFactory.newInstance().newXMLGregorianCalendar(new GregorianCalendar());
		multi.setEndDate(endDateCal);
		
		EventBasicType.Length length = new EventBasicType.Length();
		length.setMultiDay(multi);
		eventBasic.setLength(length);
		
		event.setBasic(eventBasic);
	}
	
	@Test
	public void Check_Multi_Day_in_Event_Length(){
		loginBusiness.login();
		
		try{
			eventBusiness.deleteEvent("basics013", event);
		}catch(RuntimeException e){
			logger.info("No need to delete basics013 since not found");
		}
		
		logger.info("Verify end date is earlier than stat date");
		XMLGregorianCalendar end_calendar = event.getBasic().getLength().getMultiDay().getEndDate();
		end_calendar.setDay(end_calendar.getDay()-1);
		eventBusiness.addEventUI(event);
		
		List<WebElement> errors = webDriver.findElements(By.cssSelector(".nexus-errors div.alert.alert-error>div"));
		boolean found = false;
		for(WebElement error : errors){
			String content = error.getText().trim();
			if(content.contains("before")){
				found = true;
				break;
			}
		}
		if(!found){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					Basics013.class.getName(), 
					"Verify multi-day end date is early than start date", 
					"multi-day event end date is early than start date should not allowed to create"));
		}
		
		loginBusiness.logout();
		
		
	}
}
